/**
 * Created by Administrator on 2017/7/4 0004.
 */
require(["jquery", "paging", "bootstrap", "bbcare", "jquery.dataTables", 'bootstrap.dataTables',
    "scprit/department/view/template/follow-up/common.js",
    "oms_config/root"
], function ($, Paging, bootstrap, $bb, $jTable, $bTable, $common, $root) {
    var URL = $bb.baseUrl("department") + "followup/service"
    var obj = $bb.getPageParams()
    var userId = obj.babyId
    var type = obj.type //历史记录传参；
    var patientSchemeId = obj.patientSchemeId
    var count = 1,
        pagesize = 20
    var HashMap = $common.hashmap(),
        hashMap = new HashMap()
    var historyTitle = $("#historyTitle")

    getBabyHistoryList(1, 20)
    var p = new Paging()
    p.init({
        target: '#pageTool',
        pagesize: pagesize,
        count: count,
        toolbar: false,
        callback: function (page, size, count) {
            getBabyHistoryList(page, size)
        }
    })

    $("#dataList").on('click', "ul[result]", function () {
        var data = eval("(" + hashMap.get($(this).attr("result")).replace(/\=/g, "\:") + ")")
        if (type != 'BaileyScreening') {
            var doctorName = $(this).attr("doctorName")
            var createTime = $(this).attr("testDate")
            obj.createTime = createTime
            obj.doctorName = doctorName
        }
        if (type == 'BaileyScreening') {
            var answerId = $(this).attr("result")
            obj.answerId = answerId
            $bb.openPage("baileyScreeningResult", obj)
        }
        if (type == 'Neuropsychology') {
            localStorage.setItem("Neuropsychology", JSON.stringify(data))
            $bb.openPage("childPsychologyResult", obj)
        }
        if (type == 'Temperament') {
            localStorage.setItem("temperament", JSON.stringify(data))
            $bb.openPage("temperamentResult", obj)
        }
        if (type == 'Gesell') {
            localStorage.setItem("GesellIntelligence", JSON.stringify(data))
            // $bb.openPage("gasellIntelligenceResult", obj);
        }
        if (type == 'nbna') {
            localStorage.setItem("nbna", JSON.stringify(data))
            $bb.openPage("nbnaResult", obj)
        }
        if (type == "Wechsler") {
            localStorage.setItem("smallWechsler", JSON.stringify(data))
            $bb.openPage("smallWechslerResult", obj)
        }

    })
    $("#dataList").on('click', "ul[result] li span", function () {
        var data = eval("(" + hashMap.get($(this).parent().parent().attr("result")).replace(/\=/g, "\:") + ")")
        var doctorName = $(this).parent().parent().attr("doctorName")
        var createTime = $(this).parent().parent().attr("testDate")
        obj.createTime = createTime
        obj.doctorName = doctorName
        localStorage.setItem("GesellIntelligence", JSON.stringify(data))
        if ($(this).attr('class') == "color-green cursor-pointer gesellHistoryList-questions") {
            var result = $(this).parent().parent().attr("answerResult")
            $.extend(obj, { 'result': result })
            $bb.openPage("gasellIntelligenceReport", obj)
        } else {
            $bb.openPage("gasellIntelligenceResult", obj)
        }
    })

    function getBabyHistoryList (page, rows) {
        var params = {}
        var config = $root.init()
        var showFlag = config.dom.historyWeight ? "1" : "0"
        if (type == "Wechsler") {
            params = {
                "userId": userId,
                "examinationPaperName": "Small" + type,
                "page": page,
                "rows": rows
            }
        } else {
            params = {
                "userId": userId,
                "examinationPaperName": type,
                "page": page,
                "rows": rows,
                "showFlag": showFlag
            }
        }
        if (type == 'BaileyScreening') {
            historyTitle.html("贝利神经筛查历史记录")
        }
        if (type == 'Neuropsychology') {
            historyTitle.html("小儿神经-心理检查历史记录")
        }
        if (type == 'Temperament') {
            historyTitle.html("婴幼儿气质评估历史记录")
        }
        if (type == 'Gesell') {
            historyTitle.html("Gesell智力发育评估历史记录")
        }
        if (type == 'nbna') {
            historyTitle.html("行为神经检查(NBNA)历史记录")
        }
        if (type == "Wechsler") {
            historyTitle.html("小韦氏智力量表历史记录")
        }
        $bb.ajax({
            url: URL + "/patient/evaluation/getHistoryScore", //baby地址
            async: false,
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if (data.success) {
                    // console.log(data);
                    hashMap = new HashMap()
                    count = data.total
                    var babyData = data.rows
                    if (count == 0) {
                        $bb.toastShow("患者还未参与该项测评！", 1500)
                    }
                    var str = ''
                    for (var i = 0; i < data.rows.length; i++) {
                        var testDate = data.rows[i].createTime.substring(0, 10)
                        var acturalDay = $common.DateDiff(testDate, data.rows[i].birthday)
                        var age = $common.age(acturalDay, data.rows[i].pregnancyWeek)
                        var monthAge = $common.monthAge(testDate, data.rows[i].pregnancyWeek)

                        hashMap.put(data.rows[i].answerId, data.rows[i].result)
                        var pretermBirth = 37 * 7 > data.rows[i].pregnancyWeek ? "是" : "否"
                        var weight = data.rows[i].weight ? data.rows[i].weight + "(g)" : "未知"
                        var doctorName = data.rows[i].doctorName == undefined ? "未知" : data.rows[i].doctorName
                        str += "<ul class='clearfix' answerResult='" + data.rows[i].answerResult + "' result='" + data.rows[i].answerId + "' testDate = '" + testDate + "' doctorName = '" + data.rows[i].doctorName + "' >"
                        str += '<li>' + data.rows[i].babyName + '</li>'
                        str += '<li>' + weight + '</li>'
                        str += '<li>' + data.rows[i].birthday + '</li>'
                        str += '<li>' + age + '</li>'
                        str += '<li>' + pretermBirth + '</li>'
                        str += '<li>' + doctorName + '</li>'
                        str += '<li>' + data.rows[i].createTime + '</li>'
                        if (type == "Gesell") {
                            let newEvaEle = ''
                            if (window.__config__.role.indexOf('ReadOnly') == -1) {
                                newEvaEle = '<span class="color-green cursor-pointer gesellHistoryList-questions">评估问卷</span>'
                            } else {
                                newEvaEle = ''
                            }
                            str += '<li>' + newEvaEle + '<span class="color-green cursor-pointer ml8 gesellHistoryList-report">评估报告</span></li>'
                        }
                        str += '</ul>'
                    }
                    $("#dataList").html(str)
                }
            }
        })
    }
})